﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Telerik.Web.UI;
using KPIS.GERP.GBMS.IBLL;
using KPIS.GERP.GBMS.BLL;
using KPIS.GERP.GBMS.MODEL;

namespace KPIS.GERP.GBMS.APP_UI.UserControls
{
    public partial class ExpenseDetail : System.Web.UI.UserControl
    {
        private IProject projectBLL;
        private ComboBLL comboBLL = new ComboBLL();
        private string strBgmExpGrpSeq = "";
        private string strBgmExpSeq = "";
        private string strBgmProjExpSeq = "";
        private string strBgmSourceSeq = "";
        private string strProjID = "";
        private string strParentIndex= "";

        protected void Page_Load(object sender, EventArgs e)
        {
            string strType = Session["type"].ToString();

            if (strType.Length > 0)
            {
                projectBLL = Project_Factory.GetProjectFactory(strType);
                LoadData();
            }

            //if (!IsPostBack)
            //{
            //    LoadIcmResItemSeq();
            //}
        }

        public void LoadData()
        {
            string[] strArgs = Session["ExpenseDetail"].ToString().Split('|');
            bool bSameBgmExpGrpSeq = strBgmExpGrpSeq == strArgs[0];
            strBgmExpGrpSeq = strArgs[0];
            strBgmExpSeq = strArgs[1];
            strBgmProjExpSeq = strArgs[2];
            strProjID = strArgs[3];
            strBgmSourceSeq = strArgs[4];
            strParentIndex = strArgs[5];

            if (strBgmExpGrpSeq == "3")
            {
                Panel2.Visible = true;
                Panel3.Visible = false;
                if (!bSameBgmExpGrpSeq)
                {
                    LoadDDLResItemSeq();
                }

                ddlResourceItem.SelectedValue = strArgs[7];
                txtUnitPrice.Text = strArgs[8];
                txtUnitQty.Text = strArgs[9];
            }
            else
            {
                Panel2.Visible = false;
                Panel3.Visible = true;

                txtTotalAmount.Text = strArgs[6];
            }
        }

        protected void LoadDDLResItemSeq()
        {
            ddlResourceItem.DataSource = comboBLL.ListResourceDDL(Convert.ToInt32(strBgmExpSeq));
            ddlResourceItem.DataBind();

            ddlResourceItem.Items.Insert(0, new RadComboBoxItem("-- เลือกรายการครุภัณฑ์ --", ""));
        }

        protected void btnPopupSave_Click(object sender, EventArgs e)
        {
            try
            {
                Label ParentTotalAmount = (Label)((RadGrid)Page.Form.FindControl("ALL").FindControl("pnGridExpense")
                    .FindControl("gridExpense")).Items[int.Parse(strParentIndex)]["total_amount"]
                    .FindControl("lbTotalAmount");
                if (Panel2.Visible)
                {
                    if (txtUnitPrice.Text.Length > 0
                        && txtUnitQty.Text.Length > 0
                        && ddlResourceItem.SelectedValue.Length > 0)
                    {
                        string strIcmResItemSeq = ddlResourceItem.SelectedValue;
                        int unitQty = Convert.ToInt32(txtUnitQty.Text);
                        decimal unitPrice = Convert.ToDecimal(txtUnitPrice.Text);
                        decimal totalAmount = unitQty * unitPrice;

                        projectBLL.UpdateProjectExpense(strBgmProjExpSeq, strProjID
                            , strBgmSourceSeq, strBgmExpSeq, totalAmount.ToString()
                            , txtDescr.Text, int.Parse(strIcmResItemSeq), unitQty, unitPrice
                            , null, null, null, new SsMng().GetUss(SsMng.SsUserType.EmpSeq));

                        ddlResourceItem.SelectedIndex = 0;
                        txtUnitQty.Text = "";
                        txtUnitPrice.Text = "";
                        //ClosePopup();
                        //ParentTotalAmount.Text = totalAmount.ToString();
                        //btnPopupSave.Attributes.Add("onclick", "return HideTooltipCurrent("
                        //    + ParentTotalAmount.ClientID + ", " + totalAmount.ToString() + ");");
                    }
                }
                else
                {
                    projectBLL.UpdateProjectExpense(strBgmProjExpSeq, strProjID, strBgmSourceSeq, strBgmExpSeq
                        , txtTotalAmount.Text, txtDescr.Text, null, null, null, null
                        , null, null, new SsMng().GetUss(SsMng.SsUserType.EmpSeq));
                    txtTotalAmount.Text = "";
                    //ClosePopup();
                    //ParentTotalAmount.Text = txtTotalAmount.Text;
                    //btnPopupSave.Attributes.Add("onclick", "return HideTooltipCurrent("
                    //    + ParentTotalAmount.ClientID + ", " + txtTotalAmount.Text + ");");
                }
            }
            catch (Exception ex)
            {
            }
        }

        protected void ClosePopup()
        {
            ScriptManager.RegisterClientScriptBlock(
                this.Page,
                this.GetType(),
                "WebUserControlSript",
                "CloseActiveToolTip('" + this.ID + "')",
                true);
        }
    }
}